// pages/mine/pages/my/integral/integral.js

const util = require('../../../../../utils/util');
const request = require('../../../../../utils/request')
const WxParse = require('../../../../../wxParse/wxParse/wxParse.js')
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    navlist: [{
      name: '积分记录',
      index: 1
    },{
      name: '抵扣记录',
      index: 2
    }],
    status: 1,
    currentTab: 0,
    navbarHeight: 0,
    scrollTop: 0,
    page:1,
    limit:10,
    isLastPage:false,
    list:[],
    showScore: false,
    showAgreement:false,
    isFixed: false,
    baseUrl:request.BASE_URL,
    ruleInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      navbarHeight: util.getNavbarHeight(),
      userInfo: wx.getStorageSync('userInfo')
    })
    //改变头部电量条背景色
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffffff',
    })
    this.getRuleInfo()
  },
  onShow() {
    this.setData({
      isLastPage: false,
      page:1,
      limit:10,
      list: [],
  })
    this.getList()
    this.getUserInfo()
  },
  back() {
    wx.navigateBack()
  },
  tabClick(e) {
    const index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
        currentTab: index,
        status: item.index,
        page: 1,
        list: [],
        isLastPage: false,
    })
    this.getList()
  },
  // 详情
  onDetails(e) {
    if(this.data.currentTab != 0) return
    let id = e.currentTarget.dataset.id
    //普通订单
    wx.navigateTo({
      url: '/pages/mine/pages/my/order-details/order-details?id=' + id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  onPageScroll(e){
    var that = this;
    if(e.scrollTop>=(that.data.navbarHeight+7)){
      that.setData({
        isFixed: true
      })
    }else if(e.scrollTop < that.data.navbarHeight){
      that.setData({
        isFixed: false
      })
    }
  },
  hideAgreeClose(){
    this.setData({
      showAgreement: !this.data.showAgreement
    })
  },
  hideClose(e){
    let isNew = e.detail || false
    if(this.data.userInfo.point == 0) return
    this.setData({
      showScore: !this.data.showScore
    })
    if(isNew){
      this.setData({
        page: 1,
        isLastPage: false,
      })
      this.getList()
      this.getUserInfo()
    }
  },
  //获取积分规则
  getRuleInfo() {
    let _this = this
    request._post('/api?method=point.point_desc', {},'noload').then(res => {
        _this.setData({
          ruleInfo: res.data,
        })
        WxParse.wxParse('detail_content', 'html',res.data.point_desc, _this, 5);
    })
  },
  // 获取个人信息
  getUserInfo() {
    let _this = this
    request._get('/api?method=user.info', {},'noload').then(res => {
        // console.log(res, '个人信息')
        _this.setData({
            userInfo: res.data,
        })
    })
  },
  getList() {
    let _this = this
    request._post('/api?method=user.userpointlog', {
        page: this.data.page,
        limit: this.data.limit,
        status: this.data.status
    }).then(res => {
        if (res.data.list.length == 0 || res.data.list == null) {
            _this.setData({
                isLastPage: true,
            })
        }
        _this.setData({
            list: _this.data.list.concat(res.data.list)
        })
    })
},
// 上拉加载
onReachBottom: function () {
    if (this.data.isLastPage) {
        return
    }
    let page = this.data.page
    page++
    this.setData({
        page,
    })
    this.getList()
},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})